<?php

namespace Biz\model\brandRights;

use Biz\pockets\modelTrait\BizModelTrait;
use Doraemon\model\Phone as PhoneModel;
use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\service\Member\PhoneCrypto;
use Doraemon\tools\Log;
use hhz\constant\basic\UserExtendPhoneScene;

class BrandLevelRightsLeaveInfo extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'brand_level_rights_leave_info';

    const STATUS_ON = BaseDbModel::STATUS_NORMAL;
    const STATUS_DELETE = BaseDbModel::STATUS_DELETE;

    //删除字段名称
    protected static $deleteField = 'data_status';

    public static function dataSelectInfo(array $where, array $fields = [])
    {
        $fields = empty($fields) ? '*' : $fields;
        $result = self::get($fields, $where);
        return $result;
    }

    public static function dataHas(array $where): bool
    {
        $nRet = self::has($where);
        return $nRet;
    }

    public static function dataSelect(array $where, $fields = [])
    {
        $fields = empty($fields) ? '*' : $fields;
        $result = self::select($fields, $where);
        return $result;
    }

    public static function dataCount(array $where, string $field = ''): int
    {
        $params = [];
        if (!empty($field)) {
            $params[] = $field;
        }
        $params[] = $where;
        $count = self::count(...$params);
        return $count;
    }

    public static function dataInsert(array $dataArray)
    {
        if ($dataArray['phone']) {
            $encryptPhoneId = PhoneModel::addExtendPhone($dataArray['phone'], UserExtendPhoneScene::BRAND_RIGHTS_LEAVE_INFO_PHONE);
            if (!$encryptPhoneId) {
                Log::error('添加手机号失败', [$dataArray['phone'], $encryptPhoneId]);
                throw new \Exception('系统错误，请稍后重试');
            }
            $dataArray['phone_encrypt_id'] = $encryptPhoneId;
            $dataArray['phone'] = PhoneCrypto::mask($dataArray['phone']);
        }
        $pdoStatement = self::insert($dataArray);
        if ($pdoStatement->errorCode() !== '00000') {
            return false;
        }
        return self::id();
    }

    public static function dataUpdate(array $dataArray, array $where, bool $returnRowCount = false)
    {
        if (empty($where) || empty($dataArray)) {
            return false;
        }
        if ($dataArray['phone']) {
            $encryptPhoneId = PhoneModel::addExtendPhone($dataArray['phone'], UserExtendPhoneScene::BRAND_RIGHTS_LEAVE_INFO_PHONE);
            if (!$encryptPhoneId) {
                Log::error('添加手机号失败', [$dataArray['phone'], $encryptPhoneId]);
                throw new \Exception('系统错误，请稍后重试');
            }
            $dataArray['phone_encrypt_id'] = $encryptPhoneId;
            $dataArray['phone'] = PhoneCrypto::mask($dataArray['phone']);
        }
        $pdoStatement = self::update($dataArray, $where);
        if ($pdoStatement->errorCode() !== '00000') {
            return false;
        }
        if (!$returnRowCount) {
            return true;
        }
        return $pdoStatement->rowCount();
    }


}